<template>
  <div class="app-container">
    <div class="search-container">
      <el-form
        ref="queryFormRef"
        :model="queryParams"
        :inline="true"
        class="mt-3"
      >
        <!-- <el-form-item label="省份" prop="province">
          <el-select
            v-model="queryParams.province"
            placeholder="全部"
            clearable
            class="!w-[100px]"
            filterable
          >
            <el-option
              v-for="item in provinceCode"
              :value="item.name"
              :label="item.name"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item label="接口类型" prop="queryType">
          <el-select
            v-model="queryParams.queryType"
            placeholder="全部"
            clearable
            class="!w-[100px]"
          >
            <el-option :value="-1" label="全部" />
            <el-option :value="1" label="提交手机号" />
            <el-option :value="2" label="提交验证码" />
            <el-option :value="3" label="回调" />
          </el-select>
        </el-form-item>
        <el-form-item prop="bizID" label="业务id">
          <el-input
            v-model="queryParams.bizID"
            placeholder="业务id"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item prop="bizName" label="业务名称">
          <el-input
            v-model="queryParams.bizName"
            placeholder="业务名称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item prop="bizNo" label="渠道号">
          <el-input
            v-model="queryParams.bizNo"
            placeholder="渠道号"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item prop="bizType" label="业务类型">
          <el-input
            v-model="queryParams.bizType"
            placeholder="业务类型"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item prop="mobile" label="手机号">
          <el-input
            v-model="queryParams.mobile"
            placeholder="手机号"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="订购时间范围">
          <el-date-picker
            v-model="searchTime"
            type="datetimerange"
            unlink-panels
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            @change="handleChangeTime"
            :default-time="defaultTime"
          />
        </el-form-item>

        <el-form-item prop="resCode" label="返回码">
          <el-input
            v-model="queryParams.resCode"
            placeholder="返回码"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="resetQuery"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="codeList"
        highlight-current-row
        border
        stripe
      >
        <el-table-column
          label="序号"
          type="index"
          width="60"
          :index="indexMethod"
        />
        <el-table-column label="接口类型" prop="queryType" width="120">
          <template #default="scope">
            {{ getInterfaceType(scope.row.queryType) }}
          </template>
        </el-table-column>
        <el-table-column label="手机号" prop="mobile" width="120" sortable />
        <el-table-column label="业务id" prop="queryId" width="120" sortable />
        <el-table-column label="渠道号" prop="bizNo" width="120" sortable />
        <el-table-column
          label="订购时间"
          prop="timeCreate"
          width="120"
          sortable
        />
        <el-table-column label="返回码" prop="resCode" width="120" sortable />
        <el-table-column
          label="返回描述"
          prop="resMsg"
          min-width="120"
          sortable
        />
        <el-table-column label="操作" width="360">
          <template #default="scope">
            <el-button @click="handleExport('page', scope.row)"
              >导出订单</el-button
            >
            <el-button @click="handleExport('config', scope.row)"
              >导出配置</el-button
            >
            <el-button @click="handleExport('submit', scope.row)">
              导出提交数据
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.page"
        v-model:limit="queryParams.size"
        @pagination="handleQuery"
      />
    </el-card>
  </div>
</template>
<script setup lang="ts">
import {
  getDetailsSearchPage,
  exportDetailsSearchPage,
  exportDetailsSearchConfig,
  exportDetailsSearchSubmitData,
} from "@/api/detailsSearch";
import {
  DetailsSearchQuery,
  DetailsSearchPageVO,
} from "@/api/detailsSearch/types";
import { downloadFileZip } from "@/utils/index";
import { provinceCode } from "@/utils/constant";

defineOptions({
  name: "Role",
  inheritAttrs: false,
});

const queryFormRef = ref(ElForm);

const loading = ref(false);
const total = ref(0);
const defaultTime: [Date, Date] = [
  new Date(2000, 1, 1, 0, 0, 0),
  new Date(2000, 2, 1, 23, 59, 59),
]; // '12:00:00', '08:00:00'
const indexMethod = (index: number) => {
  return index + 1;
};
const searchTime = ref(null);
const queryParams = reactive<DetailsSearchQuery>({
  page: 1,
  size: 10,
  queryType: -1,
});

const codeList = ref<DetailsSearchPageVO[]>();

function initSearchTime() {
  const start = new Date(
    new Date(
      `${new Date().getFullYear()}-${new Date().getMonth() + 1}-${new Date().getDate()} 00:00:00`
    ).getTime() -
      3600 * 1000 * 24 * 31
  );
  const end = new Date(
    `${new Date().getFullYear()}-${new Date().getMonth() + 1}-${new Date().getDate()} 23:59:59`
  );
  searchTime.value = [start, end];
  console.log("🚀 ~ initSearchTime ~ searchTime:", searchTime);
  queryParams.dateStart = new Date(
    start.getTime() + 8 * 3600 * 1000
  ).toISOString();
  queryParams.dateEnd = new Date(end.getTime() + 8 * 3600 * 1000).toISOString();
}
/** 查询 */
function handleQuery() {
  loading.value = true;
  if (queryParams.province === "全国") {
    queryParams.province = "";
  }
  getDetailsSearchPage(queryParams)
    .then((data) => {
      console.log("🚀 ~ .then ~ data:", data);
      codeList.value = data.rows;
      console.log("🚀 ~ .then ~ codeList.value:", codeList.value);
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}
/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.page = 1;
  initSearchTime();
  handleQuery();
}
function getInterfaceType(val: Number) {
  let typeName = "";
  switch (val) {
    case 1:
      typeName = "提交手机号";
      break;
    case 2:
      typeName = "提交验证码";
      break;
    case 3:
      typeName = "回调";
      break;
    default:
      typeName = val + "";
      break;
  }
  return typeName;
}
function handleChangeTime(val) {
  if (!val) {
    return;
  }
  console.log("🚀 ~ handleChangeTime ~ val:", val);
  const [start, end] = val;
  queryParams.dateStart = new Date(
    start.getTime() + 8 * 3600 * 1000
  ).toISOString();
  queryParams.dateEnd = new Date(end.getTime() + 8 * 3600 * 1000).toISOString();
}
function handleExport(type, row) {
  let api = null;
  let fileName = "明细查询";
  let exportParams = {};
  switch (type) {
    case "page":
      api = exportDetailsSearchPage;
      fileName = "订单";
      exportParams = { ...queryParams };
      break;
    case "config":
      api = exportDetailsSearchConfig;
      fileName = "配置";
      exportParams.dateStart = queryParams.dateStart;
      exportParams.dateEnd = queryParams.dateEnd;
      break;
    case "submit":
      api = exportDetailsSearchSubmitData;
      fileName = "提交数据";
      exportParams.dateStart = queryParams.dateStart;
      exportParams.dateEnd = queryParams.dateEnd;
      break;
    default:
      break;
  }
  exportParams.statu = row.resCode;
  if (api) downloadFileZip(api(exportParams));
}
onMounted(() => {
  initSearchTime();
  handleQuery();
});
</script>
